package com.liujit.common.enums;

import lombok.AllArgsConstructor;

/**
 * 返回code枚举
 *
 * @author liujun
 * @date 2021/6/15 9:31 上午
 */
@AllArgsConstructor
public enum ResultCode {
    /* 失败状态码 */
    FAILURE(0, "失败"),
    /* 成功状态码 */
    SUCCESS(1, "成功"),
    /* 参数错误 */
    PARAM_VALID_ERROR(1001, "参数验证错误"),
    PARAM_TYPE_ERROR(1002, "参数类型错误"),
    PARAM_INVALID(1003, "参数无效"),
    /* 用户错误 */
    USER_NOT_EXIST(2001, "用户不存在"),
    USER_NOT_ROLE(2002, "用户无角色，不能登录"),
    USER_NOT_LOGIN(2003, "用户未登陆"),
    USER_ACCOUNT_ERROR(2004, "用户名或密码错误"),
    USER_ACCOUNT_DISABLE(2005, "用户被禁用"),
    USER_ACCOUNT_LOCK(2006, "用户被锁定"),
    USER_EXIST(2007, "用户已存在"),
    USER_HAS_LOGIN(2008, "用户已在别处登录，谨防密码泄露"),
    USER_LOGIN_TIMEOUT(2009, "登录超时，请重新登录"),
    TOKEN_INVALID_OR_EXPIRED(2010, "token无效或已过期"),
    TOKEN_ACCESS_FORBIDDEN(2011, "token已被禁止访问"),
    CLIENT_AUTHENTICATION_FAILED(2012, "客户端认证失败"),
    /* 业务错误 */
    BUSINESS_ERROR(3001, "业务错误，请稍后再试"),
    /* 系统错误 */
    SYSTEM_INNER_ERROR(4001, "内部错误，请稍后重试"),
    /* 数据错误 */
    DATA_NOT_FOUND(5001, "数据不存在"),
    DATA_EXIST(5002, "数据已存在"),
    DATA_WRONG(5003, "数据有误"),
    /* 接口错误 */
    INTERFACE_INNER_INVOKE_ERROR(6001, "内部接口调用异常，请稍后重试"),
    INTERFACE_OUTER_INVOKE_ERROR(6002, "外部接口调用异常，请稍后重试"),
    INTERFACE_ACCESS_FORBIDDEN(6003, "该接口禁止访问"),
    INTERFACE_ADDRESS_INVALID(6004, "接口地址无效"),
    INTERFACE_REQUEST_TIMEOUT(6005, "接口请求超时"),
    INTERFACE_EXCEED_LOAD(6006, "接口负载过高"),
    INTERFACE_REPEAT_REQUEST(6007, "接口重复请求"),
    INTERFACE_ILLEGAL_REQUEST(6008, "未知请求来源，非法请求"),
    /* 权限错误 */
    PERMISSION_NO_ACCESS(7001, "无访问权限");

    private Integer code;

    private String msg;

    public static String getMessage(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.msg;
            }
        }
        return name;
    }

    public static Integer getCode(String name) {
        for (ResultCode item : ResultCode.values()) {
            if (item.name().equals(name)) {
                return item.code;
            }
        }
        return null;
    }

    public Integer code() {
        return this.code;
    }

    public String msg() {
        return this.msg;
    }
}
